import Field from 'winged/utils/wt-model-field'
import TimeUtils from 'winged/utils/wt-time-utils'
import Vue from 'vue'

export default {
	title: '手术记录',
	name: 'surgery',
	tableName: 'surgery',
	single: false,	
	query: {
		method: 'GET',
		url: 'doctor/patient/surgery',
		transfer: true,
		pagination: true,
		extraParams: [
			'case_id',
		],
		// 在修改的时候需要进行单条记录查询，用于获取关联麻醉记录
		getOnEdit: true
	},
    count: {
        method: 'GET',
        url: 'doctor/patient/surgery/count',
        transfer: true,
        extraParams: [
            'case_id',
        ],
    },
	new: {
		method: 'POST',
		url: 'doctor/patient/surgery',
		transfer: true,
		extraParams: [
			'case_id',
		]
	},
	update: {
		method: 'PUT',
		url: 'doctor/patient/surgery',
		transfer: true
	},
	delete: {
		method: 'DELETE',
		url: 'doctor/patient/surgery',
		transfer: true
	},
	id_field: {
		// 用于修改、删除操作的字段名
		name: 'surgery_id',
		// id字段查询结果中的tableName默认为'id'
		// tableName: 'id'
	},
	fields: [
		{
			label: '手术名称',
			type: Field.types.text,
			display: true,
			summary: true,//用于在概要中显示
			noSave: true,
			tableName: 'surgeryName'
		}, {
			label: '手术名称',
			type: Field.types.autocomplete,
			multiple: false,
			display: false,
			name: 'surgery_name',
			tableName: 'surgeryName',
			query: {
				url: 'doctor/patient/surgery/name',
				method: 'GET',
				transfer: true,
				pagination: true,
				useAutoFilter: true,
				allowEmptyQuery: true,
				allowAdd: true,
				addMethod: 'POST',
				addParam: 'surgery_name',
				filterParam: 'surgery_name',
				value: 'surgeryName',
				label: 'surgeryName',
			},
			rules: [{ required: true, message: '请选择手术名称', trigger: 'change' }]
		}, {
			label: '术者',
			type: Field.types.text,
			display: true,
			summary: true,//用于在概要中显示
			name: 'operator',
			tableName: 'operator',
			formSize: 'small',
			rules: [{ required: true, message: '请填写术者姓名', trigger: 'blur' }]
		}, {
			label: '手术日期',
			type: Field.types.date,
			display: true,
			name: 'surgery_date',
			tableName: 'surgeryDate',
			rules: [{ type: 'date', required: true, message: '请选择手术日期', trigger: 'blur' }]
		}, {
			label: '出血量',
			type: Field.types.number,
			unit: 'ml',
			display: true,
			summary: true,//用于在概要中显示
			formSize: 'small',
			name: 'chu_xue_liang',
			tableName: 'chuXueLiang',
			rules: [
				{ type: 'number', required: true, message: '请输入出血量', trigger: 'change' },
				{ type: 'number', message: '请输入数字' },
				{ type: 'number', min:0, trigger:'blur', message: '请输入大于零的数字'}
			]
		}, {
			label: '持续时间',
			type: Field.types.number,
			unit: '分钟',
			display: true,
			summary: true,
			formSize: 'small',
			name: 'surgery_duration',
			tableName: 'duration',
			rules: [
				{ type: 'number', required: true, message: '请输入持续时间', trigger: 'change' },
				{ type: 'number', message: '请输入数字' },
				{ type: 'number', min:0, trigger:'blur', message: '请输入大于零的数字'}
			]
		},{//表单填写的麻醉名称
			label: '麻醉名称',
			type: Field.types.select,
			multiple: true,
			allowUseWithoutAdd:true,
			display: false,
			allowAdd: true,
			name: 'anesthesia_type', 
			tableName: 'anesthesiaType',
			query: {
				url: 'doctor/patient/anesthesia/type',
				method: 'GET',
				transfer: true,
				pagination: true,
				useAutoFilter: true,
				allowEmptyQuery: true,
				filterParam: 'type_name',
				label: 'typeName',
				value: 'typeName',
				allowAdd: true,
				addMethod: 'POST',
				addParam: 'type_name',
			},
			parse:function(data){
				if(data.length == 0){
					return void(0)
				}
				let res = []
					data.forEach(item => {
						let isInclude = false//在下拉建议项options中是否含有本项
						this.originOptions.forEach(ev => {
							if(ev.typeName == item){//在建议项中含有本项
								res.push({id:ev.id,anesthesiaType:ev.typeName})
								isInclude = true
							}
						})
						if(!isInclude){//不含本项，则为用户通过"+"号新加的项，新加项id统一为0
							res.push({id:0,anesthesiaType:item})
						}
					})
				return JSON.stringify(res)
			},
			rules: [{ required: true, message: '请选择麻醉名称', trigger: 'change' ,type: 'string' }],
			getCurrentValue: function(data){
				if(!data) return [] 
				if(data.indexOf('[')>-1){
					data = JSON.parse(data).map(item => {
						return item['anesthesiaType']
					})
				}
				return data			
			}
		},{//列表显示的麻醉名称
			label: '麻醉名称',
			type: Field.types.select,
			multiple: true,
			allowUseWithoutAdd:true,
			summary: true,//用于在概要中显示
			display: true,
			noSave: true,//不在表单中显示本字项
			allowAdd: true,
			name: 'anesthesia_type', 
			filed: 'anesthesiaType',
			tableName: 'anesthesiaTypeforlist',
			query: {
				url: 'doctor/patient/anesthesia/type',
				method: 'GET',
				transfer: true,
				pagination: true,
				useAutoFilter: true,
				allowEmptyQuery: true,
				filterParam: 'type_name',
				label: 'typeName',
				value: 'typeName',
				allowAdd: true,
				addMethod: 'POST',
				addParam: 'type_name',
			},
			rules: [{ required: true, message: '请选择麻醉名称', trigger: 'change' ,type: 'string' }],
			builder: function(data){
				if(!/^\[[^\]]*\]$/.test(data[this.filed])) return data
				data[this.tableName] = JSON.parse(data[this.filed]).reduce((a, b) => {
					return a += ('/' + b[this.filed])
				}, '').substr(1) 
			}
		},{
			label: '麻醉师',
			type: Field.types.text,
			display: true,
			summary: true,//用于在概要中显示
			name: 'anesthetist',
			tableName: 'anesthetist',
			formSize: 'small',
			rules: [{ required: false, message: '请输入麻醉师姓名', trigger: 'blur' }],
			builder:function(data){
				data[this.tableName] = data.anesthetist
			}
		}/*, {
			label: '关联麻醉记录',
			type: Field.types.links,
			display: false,
			multiple: true,
			linkedModel: 'anesthesia',
			tableName: 'anesthesiaRecords',
			refreshOnLoad: true,
			customLabel: function(item) {
				return TimeUtils.format(item.anesthesiaDate, 'date') + ' ' + item.anesthesiaName;
			},
			builder: function(data) {
				if (!data[this.tableName]) {
					let records = [];
					let items = data.properties ? data.properties.AnesthesiaBeans : null;
					// 表格数据中有可能不包含麻醉记录数据
					if (!items) {
						return;
					}
					for (let i = 0; i < items.length; i ++) {
						let item = items[i];
						// records.push({
						// 	value: item.id,
						// 	label: item.anesthesiaName
						// });
						records.push(item.id);
					}
				data[this.tableName] = records;
				}
				return data[this.tableName];
			},
			uploadByComponent: true,
			upload: {
				url: 'doctor/patient/anesthesia/surgery/relation',
				method: 'POST',
				transfer: true,
				parentIdParam: 'surgery_id',
				uploadParam: 'anesthesia_id'
			},
			delete: {
				url: 'doctor/patient/anesthesia/surgery/relation',
				method: 'DELETE',
				transfer: true,
				parentIdParam: 'surgery_id',
				uploadParam: 'anesthesia_id'
			}
		}, {
			label: '麻醉记录',
			type: Field.types.text,
			display: true,
			noSave: true,
			tableName: 'anesthesiaRecordsDisplay',
			builder: function(data) {
				if (!data[this.tableName]) {
					let items = data.properties ? data.properties.AnesthesiaCount : null;
					if (items > 0) {
						data[this.tableName] = items;
					}
					else {
						data[this.tableName] = '无';
					}
				}
			}
		}*/, {
			label: '备注',
			type: Field.types.textarea,
			display: false,
			name: 'comment',
			tableName: 'comment'
		}, {
			label: '手术图片',
			type: Field.types.images,
			display: true,
			name: 'image_urls',
			tableName: 'surgeryImages',
			summary: true,
			uploadByComponent: true,
			saveAsJson: true,
			preSubmit: true,
		} 
	]
}